import request from '@/utils/request'
import {
  ResponseData,
  Trademark,
  TrademarkResponseData,
} from '@/api/product/trademark/type'
import { HttpStatusCode } from 'axios'

enum API {
  //获取已有品牌接口
  TRADEMARK_URL = '/admin/product/baseTrademark/',
  //添加品牌
  ADDTRADEMARK_URL = '/admin/product/baseTrademark/save',
  //修改已有品牌
  UPDATETRADEMARK_URL = '/admin/product/baseTrademark/update',
}

export const reqHasTrademark = async (page: number, limit: number) => {
  const ret = await request.get<TrademarkResponseData, TrademarkResponseData>(
    API.TRADEMARK_URL + `${page}/${limit}`,
  )

  if (!ret.ok || ret.code !== HttpStatusCode.Ok) {
    throw new Error(ret.message)
  }

  return ret
}

export const reqAddOrUpdateTrademark = async (data: Trademark) => {
  let ret: ResponseData
  if (data.id) {
    // 修改
    ret = await request.put<ResponseData, ResponseData>(
      API.UPDATETRADEMARK_URL,
      data,
    )
  } else {
    // 新增
    ret = await request.post<ResponseData, ResponseData>(
      API.ADDTRADEMARK_URL,
      data,
    )
  }

  if (!ret.ok || ret.code !== HttpStatusCode.Ok) {
    throw new Error(ret.message)
  }

  return ret
}
